package com.project.petLossSystem.controller.system;

import com.project.petLossSystem.common.Constants;
import com.project.petLossSystem.service.IndexCarouselService;
import com.project.petLossSystem.service.PetPostService;
import com.project.petLossSystem.vo.IndexCarouselVO;
import com.project.petLossSystem.vo.petPost.IndexPetPostVO;
import com.project.petLossSystem.vo.petPost.SearchPetPostVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collections;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private IndexCarouselService indexCarouselService;

    @Autowired
    private PetPostService petPostService;

    @GetMapping({"/", "/index", "/index.html"})
    public String index(Model model) {
        /* 轮播图 */
        List<IndexCarouselVO> carouselVOs = indexCarouselService.getCarouselsForIndex();
        // 为空则给默认图片
        if (CollectionUtils.isEmpty(carouselVOs)) {
            IndexCarouselVO carouselVO = new IndexCarouselVO();
            carouselVO.setCarouselUrl("/carousel/local/404.jpeg");
            carouselVO.setRedirectUrl("/404");
            model.addAttribute("carousels", Collections.singletonList(carouselVO));
        } else {
            model.addAttribute("carousels", carouselVOs);
        }
        /* 热搜帖子 */
        List<IndexPetPostVO> indexPetPostVOS = petPostService.getIndexPetPostVO(Constants.INDEX_POST_COUNT_FOR_VIEWS);
        if (!CollectionUtils.isEmpty(indexPetPostVOS)) {
            model.addAttribute("indexPetPostVOS", indexPetPostVOS);
        }
        /* 新帖子 */
        List<SearchPetPostVO> newLostPosts = petPostService.getNewPostsForIndex(Constants.INDEX_NEW_POST_COUNT, (byte) 0);
        if(!CollectionUtils.isEmpty(newLostPosts)){
            model.addAttribute("newLostPosts", newLostPosts);
        }
        List<SearchPetPostVO> newFoundPosts = petPostService.getNewPostsForIndex(Constants.INDEX_NEW_POST_COUNT, (byte) 1);
        if(!CollectionUtils.isEmpty(newFoundPosts)){
            model.addAttribute("newFoundPosts", newFoundPosts);
        }
        return "/pc/system/index";
    }
}
